package vn.fpt.fsoft.action.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.web.struts.DispatchActionSupport;

import vn.fpt.fsoft.bl.ManageUsersBL;
import vn.fpt.fsoft.entity.AdminAccountInfo;
import vn.fpt.fsoft.entity.UserAccountInfo;

public class DeleteAdminAction extends DispatchActionSupport {
	public ActionForward deleteAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		AdminAccountInfo admin = null;
		try {
			ManageUsersBL manageUserBL = (ManageUsersBL) getWebApplicationContext()
					.getBean("ManageUserBL");
			DynaActionForm f = (DynaActionForm) form;
			String adminId = request.getParameter("adminId");
			List<AdminAccountInfo> list = manageUserBL.getAdminById(adminId);
			if (list.size() < 1) {
				return mapping.findForward("delete_admin_failure");
			} else {
				admin = list.get(0);

				f.set("admin", admin);
			}
			// // Please delete line below
			// //////////////////////////////////////////////////////////////////////////
			request.getSession().setAttribute("role", "Admin");// //////////////////////
			// ////////////////////////////////////////////////////////////////////////

			return mapping.findForward("success_admin");
		} catch (Exception ex) {
			ex.printStackTrace();
			if (admin.getRole() == 1) {
				return mapping.findForward("delete_admin_failure");
			} else {
				return mapping.findForward("delete_contributer_failure");
			}
		}
	}

	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			ManageUsersBL manageUserBL = (ManageUsersBL) getWebApplicationContext()
					.getBean("ManageUserBL");
			DynaActionForm f = (DynaActionForm) form;
			String userId = request.getParameter("userId");
			List<UserAccountInfo> list = manageUserBL.getUserById(userId);
			if (list.size() < 1) {
				return mapping.findForward("delete_user_failure");
			} else {
				UserAccountInfo user = list.get(0);

				f.set("user", user);
			}
			return mapping.findForward("success_user");
		} catch (Exception ex) {
			ex.printStackTrace();
			return mapping.findForward("delete_user_failure");
		}
	}

}
